package com.netty.example.nio;

import java.io.File;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;

public class NIOTest12 {
    public static void main(String[] args) throws Exception{
        String inputFileName = "NIOTest12_input.txt";
        String outputFileName = "NIOTest12_output.txt";
        RandomAccessFile inputFile = new RandomAccessFile(inputFileName, "r");
        RandomAccessFile outputFile = new RandomAccessFile(outputFileName, "rw");
        long inputLength = new File(inputFileName).length();
        FileChannel inputChannel = inputFile.getChannel();
        FileChannel outputChannel = outputFile.getChannel();
        MappedByteBuffer inputData = inputChannel.map(FileChannel.MapMode.READ_ONLY, 0, inputLength);
        Charset charset = Charset.forName("utf-8");
        CharsetDecoder decoder = charset.newDecoder();
        CharsetEncoder encoder = charset.newEncoder();
        CharBuffer charBuffer = decoder.decode(inputData);
        ByteBuffer outputData = encoder.encode(charBuffer);
        outputChannel.write(outputData);
        inputFile.close();
        outputFile.close();

    }
}
